package com.yanqu.road.server.cmd.activity.douluoshrek;

import com.yanqu.road.entity.activity.douluoshrek.LevelUpStudentResult;
import com.yanqu.road.pb.activity.DouLuoShrekProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shrek.DouLuoShrekModule;
import com.yanqu.road.server.manger.activity.shrek.DouLuoShrekMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;


@Cmd(code = Protocol.S_DOULUOSHREK_BUILDING_LEVEL_UP, desc = "史瑞克建筑升级")
public class ShrekBuildingLevelUpCmd extends AbstractCommand {

    private int activityId;
    private int buildingId;
    private int type;//1代表升级坑位的那个技能，2是每个建筑的特殊技能那个

    private LevelUpStudentResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoShrekProto.LevelUpBuildingReqMsg reqMsg = DouLuoShrekProto.LevelUpBuildingReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        buildingId = reqMsg.getBuildingId();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(DouLuoShrekMgr.getActivityTimeAcId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!DouLuoShrekMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        Object locker = player.getModule(DouLuoShrekModule.class).getLocker();
        synchronized (locker){
            try {
                result = player.getModule(DouLuoShrekModule.class).levelUp(buildingId, type);
                ret = result.getRet();
            } catch (Exception e) {
                ret = GameErrorCode.E_ERROR_DATA;
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        DouLuoShrekProto.LevelUpBuildingRespMsg.Builder respMsg = DouLuoShrekProto.LevelUpBuildingRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            DouLuoShrekProto.BuildingTemp.Builder buildingTempMsg = DouLuoShrekProto.BuildingTemp.newBuilder();
            buildingTempMsg.setBuildingId(buildingId);
            buildingTempMsg.setBuildingLevel(player.getModule(DouLuoShrekModule.class).getBuildingLevel(buildingId));
            buildingTempMsg.setSkillLevel(player.getModule(DouLuoShrekModule.class).getSkillLevel(buildingId));
            Map<Integer,Long> timeMap = player.getModule(DouLuoShrekModule.class).getBuildingLevelUpTimeMap(buildingId);
            for(long levelUpTime : timeMap.values()){
                buildingTempMsg.addLevelUpTime(levelUpTime);
            }
            respMsg.setBuilding(buildingTempMsg);
        }
        player.sendPacket(Protocol.U_DOULUOSHREK_BUILDING_LEVEL_UP, respMsg);
        //变化的计算一次
        player.getModule(DouLuoShrekModule.class).syncStudent(result.getChangeStudent(),type);
    }
}
